import Utils from "../../../../../Utils";
import { FacilityType } from "../../../../info/GameEnum";
import { Global } from "../../../../info/Global";
import { Location } from "../../../../info/Predefine";
import { AiEnvironment } from "../../AiGameAction";
import { DecisionNode } from "../../DecisionTree";


/**
 * 建造攻击设施
 */
export default class DecisionBuildAttackBase extends DecisionNode {

    environment: AiEnvironment & {
        spaces1: Location[][], sheelScore: number,
    };

    constructor(environment: any) {
        super(environment);
        this.childDecisionNodes.push(new BuildHowitzerBase(environment));
        this.childDecisionNodes.push(new BuildRocketBase(environment));
        this.childDecisionNodes.push(new BuildElectricBase(environment));
        this.childDecisionNodes.push(new BuildIntercontinentalMissileBase(environment));
        this.childDecisionNodes.push(new BuildNuclearBombBase(environment));
    }

    evaluate(): number {
        let score = 0;
        let spaces = this.environment.player.getSpaceBySize(1);
        if (spaces.length) {
            score += 100 - Math.min(100, this.environment.sheelScore);
            this.environment.spaces1 = spaces;
        }
        return score;
    }
}


/**
 * 建造榴弹炮基地
 */
class BuildHowitzerBase extends DecisionNode {

    environment: AiEnvironment & {
        spaces1: Location[][], sheelScore: number,
    };

    evaluate(): number {
        if (!this.environment.facilityFeasibility.get(FacilityType.榴弹炮基地).every(i => i))
            return 0;
        let facilityInfo = Global.getFacilityInfo<{ 威胁值: number, 每主基地等级可建造上限: number[] }>(FacilityType.榴弹炮基地);
        //获取上限
        let buildLimit = facilityInfo.每主基地等级可建造上限[this.environment.player.mainHomeLevel];
        let currCount = this.environment.player.facilityArr[FacilityType.榴弹炮基地].length;
        return (buildLimit - currCount) * facilityInfo.威胁值;
    }

    execute(): number {
        this.environment.player.addFacility(FacilityType.榴弹炮基地, Utils.getRandomChoose(this.environment.spaces1));
        return 0;
    }
}

/**
 * 建造火箭炮基地
 */
class BuildRocketBase extends DecisionNode {

    environment: AiEnvironment & {
        spaces4: Location[][]; sheelScore: number,
    };


    evaluate(): number {
        if (!this.environment.facilityFeasibility.get(FacilityType.火箭炮基地).every(i => i))
            return 0;

        let spaces = this.environment.player.getSpaceBySize(4);
        if (!spaces.length)
            return 0;
        this.environment.spaces4 = spaces;

        let facilityInfo = Global.getFacilityInfo<{ 威胁值: number, 每主基地等级可建造上限: number[] }>(FacilityType.火箭炮基地);
        //获取上限
        let buildLimit = facilityInfo.每主基地等级可建造上限[this.environment.player.mainHomeLevel];
        let currCount = this.environment.player.facilityArr[FacilityType.火箭炮基地].length;
        return (buildLimit - currCount) * facilityInfo.威胁值;
    }

    execute(): number {
        this.environment.player.addFacility(FacilityType.火箭炮基地, Utils.getRandomChoose(this.environment.spaces4));
        return 0;
    }

}

/**
 * 建造电磁炮基地
 */
class BuildElectricBase extends DecisionNode {

    environment: AiEnvironment & {
        spaces4: Location[][]; sheelScore: number,
    };


    evaluate(): number {
        if (!this.environment.facilityFeasibility.get(FacilityType.电磁炮基地).every(i => i))
            return 0;

        let spaces = this.environment.player.getSpaceBySize(4);
        if (!spaces.length)
            return 0;
        this.environment.spaces4 = spaces;

        let facilityInfo = Global.getFacilityInfo<{ 威胁值: number, 每主基地等级可建造上限: number[] }>(FacilityType.电磁炮基地);
        //获取上限
        let buildLimit = facilityInfo.每主基地等级可建造上限[this.environment.player.mainHomeLevel];
        let currCount = this.environment.player.facilityArr[FacilityType.电磁炮基地].length;
        return (buildLimit - currCount) * facilityInfo.威胁值;
    }

    execute(): number {
        this.environment.player.addFacility(FacilityType.电磁炮基地, Utils.getRandomChoose(this.environment.spaces4));
        return 0;
    }

}

/**
 * 建造洲际导弹基地
 */
class BuildIntercontinentalMissileBase extends DecisionNode {

    environment: AiEnvironment & {
        spaces3: Location[][]; sheelScore: number,
    };

    evaluate(): number {
        if (!this.environment.facilityFeasibility.get(FacilityType.洲际导弹基地).every(i => i))
            return 0;

        let spaces = this.environment.player.getSpaceBySize(3);
        if (!spaces.length)
            return 0;
        this.environment.spaces3 = spaces;

        let facilityInfo = Global.getFacilityInfo<{ 威胁值: number, 每主基地等级可建造上限: number[] }>(FacilityType.洲际导弹基地);
        //获取上限
        let buildLimit = facilityInfo.每主基地等级可建造上限[this.environment.player.mainHomeLevel];
        let currCount = this.environment.player.facilityArr[FacilityType.洲际导弹基地].length;
        return (buildLimit - currCount) * facilityInfo.威胁值;
    }

    execute() {
        this.environment.player.addFacility(FacilityType.洲际导弹基地, Utils.getRandomChoose(this.environment.spaces3));
        return 0;
    }
}

/**
 * 建造核弹基地
 */
class BuildNuclearBombBase extends DecisionNode {
    environment: AiEnvironment & {
        spaces4: Location[][]; sheelScore: number,
    };


    evaluate(): number {
        if (!this.environment.facilityFeasibility.get(FacilityType.核弹基地).every(i => i))
            return 0;

        let spaces = this.environment.player.getSpaceBySize(4);
        if (!spaces.length)
            return 0;
        this.environment.spaces4 = spaces;

        let facilityInfo = Global.getFacilityInfo<{ 威胁值: number, 每主基地等级可建造上限: number[] }>(FacilityType.核弹基地);
        //获取上限
        let buildLimit = facilityInfo.每主基地等级可建造上限[this.environment.player.mainHomeLevel];
        let currCount = this.environment.player.facilityArr[FacilityType.核弹基地].length;
        return (buildLimit - currCount) * facilityInfo.威胁值;
    }

    execute(): number {
        this.environment.player.addFacility(FacilityType.核弹基地, Utils.getRandomChoose(this.environment.spaces4));
        return 0;
    }

}
